﻿@{
    ViewBag.Title = "文章辩选";
}
@model IEnumerable<Article>

<table class="layui-table layui-form">
    <thead>
        <tr>
            <th><input type="checkbox" lay-filter="all" name="IDAll" lay-skin="primary"> ID</th>
            <th>标题</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
    </thead>
    @foreach (var item in Model)
    {
        <tr>
            <td><input type="checkbox" value="@item.ID" lay-filter="item" name="ID" lay-skin="primary"> @item.ID</td>
            <td title="@item.Content">
                @item.Title
            </td>
            <td>
                @item.CreateTime
            </td>
            <td>
                <a href="/articles/delete/@item.ID" class="del-btn">删除</a>
            </td>
        </tr>
    }
</table>
<button id="merge" class="layui-btn">合并</button><a href="@Request["returnUrl"]" class="layui-btn layui-btn-primary">返回列表</a>
@using Webdiyer.WebControls.Mvc;
@Html.Pager(new PagedList<ArticleWeb.Models.Article>(Model, Request.GetPageIndex(), Request.GetPageSize(), ViewBag.Total), new PagerOptions
{
    PageIndexParameterName = "page",
    CurrentPagerItemWrapperFormatString = "<span class=\"layui-laypage-curr\"><em class=\"layui-laypage-em\"></em><em>{0}</em></span>",
}, new { @class = "layui-box layui-laypage layui-laypage-default" })
@section scripts{
    @if (Model.Count() <= 1)
    {
        <script>
            location.href = '@Request["returnUrl"]';
        </script>
    }
    else
    {
        <script>
            var ids = [];
            $('#merge').click(function () {
                ids = [];
                $('[name=ID]:checked').each(function () { ids.push($(this).val()); });
                if (ids.length < 2) {
                    layer.msg('要合并文章，至少要选择两篇文章');
                    return;
                }
                else {
                    $.post('/articles/merge', { ids: JSON.stringify(ids) }, function (d) {
                        layer.msg(d.msg);
                        if (d.code == 200) {
                            location.reload();
                        }
                    });
                }
            });
            layui.define(['form', 'element'], function () {
                var form = layui.form();
                //var element = layui.element();
                form.on('checkbox(all)', function (data) {
                    if (data.elem.checked) {
                        $('[name=ID]').prop('checked', true);
                    }
                    else {
                        $('[name=ID]').removeAttr("checked")
                    }
                    form.render('checkbox')//setTimeout(function () {  }, 200);
                });
                form.on('checkbox(item)', function (data) {
                    var total = $('[name=ID]').length;
                    var checked = $('[name=ID]:checked').length
                    if (total == checked) {
                        $('[name=IDAll]').prop('checked', true);
                    }
                    else {
                        $('[name=IDAll]').removeAttr("checked")
                    }
                    form.render('checkbox')//setTimeout(function () {  }, 200);

                });
            })
        </script>
    }
}